import { reactive } from 'vue';
import { getProductManualConfig, getProductManualTags, searchProductManualList, getProductDetailById } from '@/api/server';
import { apiService } from '@/api/apiService';
import { ssm } from '@/util/ServerLang';

export function useProductSetup () {
    const liststate = reactive({
        labels: [],
        loading: false,
        isfinished: false,
        searchmodel: {
            p: 1,
            n: 10,
            key: '',
            tags: []
        },
        productlist: []
    });

    const productmanual = reactive({
        config: {}
    });

    const product = reactive({
        loading: true,
        detail: {}
    });
    const getProductLabels = () => {
        apiService({
            api: getProductManualTags,
            param: {
                groupid: 'product_manuals'
            },
            success: (data) => {
                liststate.labels = data.value;
            }
        });
    };
    const getProductList = () => {
        liststate.loading = true;
        apiService({
            api: searchProductManualList,
            param: {
                ...liststate.searchmodel
            },
            success: (data) => {
                // 如果是第一页请求需要清空数据
                if (liststate.searchmodel.p === 1) {
                    liststate.productlist = null;
                    liststate.productlist = data.value;
                } else {
                    liststate.productlist = liststate.productlist.concat(data.value);
                }
                liststate.searchmodel.p++;

                if (data.value.length < liststate.searchmodel.n) {
                    liststate.isfinished = true;
                }
            },
            fail: (data) => {
                ssm(data);
                liststate.isfinished = true;
            },
            end: () => {
                liststate.loading = false;
            }
        });
    };

    const getProductConfig = () => {
        apiService({
            api: getProductManualConfig,
            param: {

            },
            success: (data) => {
                productmanual.config = data.value;
            }
        });
    };

    const getProductDetail = (id, callback) => {
        apiService({
            api: getProductDetailById,
            param: {
                id
            },
            success: (data) => {
                product.detail = data.value;
                product.loading = false;

                callback && callback();
            },
            fail: (data) => {
                if (data.errorno === 1135004) {
                    product.detail = { status: 'deleted' };
                    product.loading = false;
                } else {
                    ssm(data);
                }
            }
        });
    };

    const getCoverpicture = (product) => {
        return product.coverpicture && product.coverpicture[0] && product.coverpicture[0].fileurl;
    };
    return {
        liststate,
        product,
        productmanual,
        getCoverpicture,
        getProductConfig,
        getProductDetail,
        getProductLabels,
        getProductList
    };
}
